<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Card extends Base
{
	//保修卡列表
	public function list()
	{

		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					// $list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->order('id desc')->limit($start, $limit)->select();
					// $count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->count();
                    $one='';//关联序列号查询
                    $f=Db::name('tagslogs')->where('sn',$data['search'])->find();
                    if(!empty($f)&&$f['snkey']==1){
                        $msn=more_sn($data['search']);
                        if($msn['status']==1){
                            $one=$msn['one'];
                        }
                    }
                  
					$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)
                     ->union(function($query)use($one){
                        $query->name('card')->where('tagslogs_sn',$one);
                    })
                    ->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					// $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->limit($start, $limit)->select();
					// $count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->limit($start, $limit)->count();
					$list =Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				//$list = Db::name('card')->where('checkstatus', 1)->order('id desc')->limit($start, $limit)->select();
				//$count = Db::name('card')->where('checkstatus', 1)->count();

				$list =Db::name('card')->where('checkstatus', 1)->order('id desc')->select();
				$list=more_sn_one($list,'tagslogs_sn');
				$count=count($list);
				$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['status'] = '未过期';
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];
				if (time() > $value['endtime']) {
					$list[$key]['status'] = '已过期';
				}

				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}

				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}
			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	// 未过期
	public function in_time()
	{

		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					// $list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '>', time())->order('id desc')->limit($start, $limit)->select();
					// $count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '>', time())->count();

					$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '>', time())->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					// $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '>', time())->limit($start, $limit)->select();
					// $count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '>', time())->limit($start, $limit)->count();
					$list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '>', time())->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				// $list = Db::name('card')->where('checkstatus', 1)->where('endtime', '>', time())->order('id desc')->limit($start, $limit)->select();
				// $count = Db::name('card')->where('checkstatus', 1)->where('endtime', '>', time())->count();
					$list = Db::name('card')->where('checkstatus', 1)->where('endtime', '>', time())->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];
				$list[$key]['status'] = '未过期';

				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}

				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}
			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	public function in_time_daochu()
	{
		$data = $this->request->param();
		if (!empty($data['stime']) && !empty($data['etime'])) {
			$data['stime'] = strtotime($data['stime'] . " 00:00:00");
			$data['etime'] = strtotime($data['etime'] . " 23:59:59");
			if($data['etime'] < time() ){
				$data['etime'] = time();
			}
			$lists = Db::name('card')->order('id desc')->where('starttime', '>=', $data['stime'])->where('endtime', '>', time())->where('endtime', '<=', $data['etime'])->select();
		} else {
			$lists = Db::name('card')->where('endtime', '>', time())->order('id desc')->select();
		}
		
		foreach ($lists as $key => $value) {
			// 查询标签
			$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
			$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
			$good = Db::name('good')->where('id', $good_id)->find();
			$lists[$key]['good_name'] = $good['name'];
			$lists[$key]['goodstags_sn'] = $goodstags['sn'];
			$lists[$key]['good_model'] = $good['model'];
			$lists[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
			$lists[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
			$lists[$key]['status'] = '未过期';
			if (time() > $value['endtime']) {
				$list[$key]['status'] = '已过期';
			}

			switch ($value['checkstatus']) {
				case 0:
					$lists[$key]['checkstatus_name'] = '待审核';
					break;
				case 1:
					$lists[$key]['checkstatus_name'] = '通过';
					break;
				case 2:
					$lists[$key]['checkstatus_name'] = '拒绝';
					break;
			}
		}

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("保修卡数据"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "保修状态")
			->setCellValue("B1", "审核状态")
			->setCellValue("C1", "保修开始")
			->setCellValue("D1", "保修截止")
			->setCellValue("E1", "序列号")
			->setCellValue("F1", "商品名称")
			->setCellValue("G1", "产品型号")
			->setCellValue("H1", "保修卡编号")
			->setCellValue("I1", "单位")
			->setCellValue("J1", "联系人")
			->setCellValue("K1", "电话")
			->setCellValue("L1", "手机")
			->setCellValue("M1", "电子邮件");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['status'])
				->setCellValue("B" . ($key + 2), $val['checkstatus_name'])
				->setCellValue("C" . ($key + 2), $val['starttime'])
				->setCellValue("D" . ($key + 2), $val['endtime'])
				->setCellValue("E" . ($key + 2), $val['goodstags_sn'])
				->setCellValue("F" . ($key + 2), $val['good_name'])
				->setCellValue("G" . ($key + 2), $val['good_model'])
				->setCellValue("H" . ($key + 2), $val['bianhao'])
				->setCellValue("I" . ($key + 2), $val['danwei'])
				->setCellValue("J" . ($key + 2), $val['user'])
				->setCellValue("K" . ($key + 2), $val['phone'])
				->setCellValue("L" . ($key + 2), $val['mobile_phone'])
				->setCellValue("M" . ($key + 2), $val['email']);
			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(30);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="保修卡数据' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}

	// 已过期
	public function out_time()
	{

		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					//$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '<', time())->order('id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '<', time())->count();
                    $list =  Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 1)->where('endtime', '<', time())->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					// $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '<', time())->limit($start, $limit)->select();
					// $count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '<', time())->limit($start, $limit)->count();
					$list =  Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 1)->order('a.id desc')->where('a.endtime', '<', time())->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				// $list = Db::name('card')->where('checkstatus', 1)->where('endtime', '<', time())->order('id desc')->limit($start, $limit)->select();
				// $count = Db::name('card')->where('checkstatus', 1)->where('endtime', '<', time())->count();
				$list = Db::name('card')->where('checkstatus', 1)->where('endtime', '<', time())->order('id desc')->select();
				$list=more_sn_one($list,'tagslogs_sn');
				$count=count($list);
				$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['status'] = '已过期';
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];

				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}

				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}
			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	public function out_time_daochu()
	{
		$data = $this->request->param();
		if (!empty($data['stime']) && !empty($data['etime'])) {
			$data['stime'] = strtotime($data['stime'] . " 00:00:00");
			$data['etime'] = strtotime($data['etime'] . " 23:59:59");
			if($data['etime'] > time() ){
				$data['etime'] = time();
			}
			$lists = Db::name('card')->order('id desc')->where('checkstatus', 1)->where('starttime', '>=', $data['stime'])->where('endtime', '<=', $data['etime'])->select();
		} else {
			$lists = Db::name('card')->where('checkstatus', 1)->where('endtime', '<', time())->order('id desc')->select();
		}

		foreach ($lists as $key => $value) {
			// 查询标签
			$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
			$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
			$good = Db::name('good')->where('id', $good_id)->find();
			$lists[$key]['good_name'] = $good['name'];
			$lists[$key]['goodstags_sn'] = $goodstags['sn'];
			$lists[$key]['good_model'] = $good['model'];
			$lists[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
			$lists[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
			$lists[$key]['status'] = '未过期';
			if (time() > $value['endtime']) {
				$list[$key]['status'] = '已过期';
			}

			switch ($value['checkstatus']) {
				case 0:
					$lists[$key]['checkstatus_name'] = '待审核';
					break;
				case 1:
					$lists[$key]['checkstatus_name'] = '通过';
					break;
				case 2:
					$lists[$key]['checkstatus_name'] = '拒绝';
					break;
			}
		}

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("保修卡数据"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "保修状态")
			->setCellValue("B1", "审核状态")
			->setCellValue("C1", "保修开始")
			->setCellValue("D1", "保修截止")
			->setCellValue("E1", "序列号")
			->setCellValue("F1", "商品名称")
			->setCellValue("G1", "产品型号")
			->setCellValue("H1", "保修卡编号")
			->setCellValue("I1", "单位")
			->setCellValue("J1", "联系人")
			->setCellValue("K1", "电话")
			->setCellValue("L1", "手机")
			->setCellValue("M1", "电子邮件");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['status'])
				->setCellValue("B" . ($key + 2), $val['checkstatus_name'])
				->setCellValue("C" . ($key + 2), $val['starttime'])
				->setCellValue("D" . ($key + 2), $val['endtime'])
				->setCellValue("E" . ($key + 2), $val['goodstags_sn'])
				->setCellValue("F" . ($key + 2), $val['good_name'])
				->setCellValue("G" . ($key + 2), $val['good_model'])
				->setCellValue("H" . ($key + 2), $val['bianhao'])
				->setCellValue("I" . ($key + 2), $val['danwei'])
				->setCellValue("J" . ($key + 2), $val['user'])
				->setCellValue("K" . ($key + 2), $val['phone'])
				->setCellValue("L" . ($key + 2), $val['mobile_phone'])
				->setCellValue("M" . ($key + 2), $val['email']);
			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(30);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="保修卡数据' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}

	public function add()
	{
		return $this->fetch();
	}

	//延期1-1
	public function yanqi($id)
	{

		$info = Db::name('card')->where('id', $id)->find();
		$info['endtime'] = date('Y-m-d', $info['endtime']);
		$this->assign('info', $info);
		return $this->fetch();
	}

	//延期1-2
	public function yanqi_save()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		// 启动事务
		Db::startTrans();
		$up2 = [
			'endtime' => Db::name('card')->where('id', $data['id'])->value('endtime'),
			'starttime' => strtotime($data['starttime'] . ' 00:00:00'),
			'addtime' => time(),
			'card_id' => $data['id'],
			'newendtime' => strtotime($data['endtime'] . ' 23:59:59'),
			'admin_id' => Session::get('user_id'),
		];
		
		$res = Db::name('cardlogs')->insert($up2);
		if(!$res){
			// 回滚事务
			Db::rollback();
			return ['status' => $status, 'msg' => $msg];
		}
		$up1 = [
			'starttime' => strtotime($data['starttime'] . ' 00:00:00'),
			'endtime' => strtotime($data['endtime'] . ' 23:59:59'),
		];
		$res1 = Db::name('card')->where('id', $data['id'])->update($up1);
		if (!$res1) {
			// 回滚事务
			Db::rollback();
			return ['status' => $status, 'msg' => $msg];
		}
		// 查询当前序列号保修卡信息
		$info = Db::name('card')->where('id',$data['id'])->find();
		$info['starttime'] = strtotime($data['starttime'] . ' 00:00:00');
		$info['endtime'] = strtotime($data['endtime'] . ' 23:59:59');
		// 查询是否多个序列号
		$more_sn = more_sn($info['tagslogs_sn']);
		if($more_sn['status'] == 1){
			// 查询另一个保修卡是否存在
			$other_card = Db::name('card')->where('tagslogs_sn','in',$more_sn['sn'])->count();
			$sn_arr = explode(',', $more_sn['sn']);
			if($other_card == 0){
				unset($info['id']);
				foreach ($sn_arr as $key => $value) {
					$info['tagslogs_sn'] = $value;
					$insert = Db::name('card')->insert($info);
					if(!$insert){
						// 回滚事务
						Db::rollback();
						return ['status' => $status, 'msg' => $msg];
					}
				}
				
			}else{

				foreach ($sn_arr as $key => $value) {
					// 判断对应序列号的保修卡是否存在
					$card_info = Db::name('card')->where('tagslogs_sn',$value)->find();
					if(!empty($card_info)){
						$update = Db::name('card')->where('tagslogs_sn',$value)->update($up1);
						if($update === false){
							// 回滚事务
							Db::rollback();
							return ['status' => $status, 'msg' => $msg];
						}
					}else{

						unset($info['id']);
						$info['tagslogs_sn'] = $value;
						$insert = Db::name('card')->insert($info);
						if(!$insert){
							// 回滚事务
							Db::rollback();
							return ['status' => $status, 'msg' => $msg];
						}

					}
					
				}
			}

		}

		// 提交事务
		Db::commit();
		$status = 1;
		$msg = "操作成功";
		return ['status' => $status, 'msg' => $msg];
	}
	//保修条款1-1
	public function report()
	{
		$info=Db::name('system')->where('id',1)->find();
		$this->assign('info',$info);
		return $this->fetch();
	}
	//保修条款1-2
	public function report_save()
	{
		$status=0;
        $msg="操作失败";
        $data = $this->request->param();
        $data['updatetime']=time();
        $res=Db::name('system')->where('id',1)->update($data);
        if($res){
        	adminlog('修改系统配置','修改');
        	$status=1;
        	$msg="修改成功";
        }
        return ['status'=>$status, 'msg'=> $msg];
	}


	//编辑栏目1-2
	public function save()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		$data['tagslogs_sn'] = trim($data['tagslogs_sn']);
		if (empty($data['tagslogs_sn'])) {
			return ['status' => $status, 'msg' => '序列号不能为空'];
		}

		if (empty($data['endtime'])) {
			return ['status' => $status, 'msg' => '保修期限不能为空'];
		}


		$data['endtime'] = strtotime($data['endtime'] . " 23:59:59");
		$data['starttime'] = strtotime($data['starttime'] . " 00:00:00");
		$data['addtime'] = time();
		if ($data['endtime'] < $data['starttime']) {
			return ['status' => $status, 'msg' => '保修期限小于当前日期'];
		}

		$info = Db::name('tagslogs')->where('sn', $data['tagslogs_sn'])->find();
		if (empty($info)) {
			return ['status' => $status, 'msg' => '请输入正确的序列号'];
		}
		$card = Db::name('card')->where('tagslogs_sn', $data['tagslogs_sn'])->find();
		if (!empty($card) && $card['checkstatus'] == 1) {
			return ['status' => $status, 'msg' => '该序列号商品已经存在保修卡'];
		} elseif (!empty($card) && $card['checkstatus'] == 0) {
			return ['status' => $status, 'msg' => '保修激活审核中'];
		}
		$data['bianhao'] = bh();
		$data['checkstatus'] = 1;
		

		//新增绑定保修卡详情
		$pid=Db::name('card_record')->order('id desc')->value('id');
		if(empty($pid)){
			$pid=1;
		}
		$data['pid']=$pid;
		$data['admin_id'] = Session::get('user_id');
		
		$rest = Db::name('card')->insert($data);
		if ($rest) {
			$status = 1;
			$msg = "添加成功";
		}
		adminlog("添加维修卡", '添加');
		return ['status' => $status, 'msg' => $msg];
	}

	// 编辑保修卡
	public function edit_card()
	{
		$data = $this->request->param();
		$info = Db::name('card')->where('id',$data['id'])->find();
		$info['starttime'] = date('Y-m-d', $info['starttime']);
		$info['endtime'] = date('Y-m-d', $info['endtime']);

		$this->assign('info',$info);
		return $this->fetch();
	}

	// 编辑保修卡
	public function update_card()
	{
		$status = 1;
		$msg = "编辑成功";
		$data = $this->request->param();
		$data['starttime'] = strtotime($data['starttime'] . " 00:00:00");
		$data['endtime'] = strtotime($data['endtime'] . " 23:59:59");
		$data['updatetime'] = time();
		// 启动事务
		Db::startTrans();
		// 更新当前保修卡
		$post = $data;
		unset($data['tagslogs_sn']);
		unset($data['id']);
		$rest = Db::name('card')->where('id',$post['id'])->update($data);
		if(!$rest){
			$status = 0;
			$msg = "编辑失败";
			// 回滚事务
			Db::rollback();
			return ['status' => $status, 'msg' => $msg];
		}
		// 查询更新后的保修卡
		$info = Db::name('card')->where('id',$post['id'])->find();
		// 查询是否多个序列号
		$more_sn = more_sn($post['tagslogs_sn']);
		if($more_sn['status'] == 1){
			// 查询另一个保修卡是否存在
			$other_card = Db::name('card')->where('tagslogs_sn','in',$more_sn['sn'])->count();
			$sn_arr = explode(',', $more_sn['sn']);
			if($other_card == 0){
				unset($info['id']);
				foreach ($sn_arr as $key => $value) {
					$info['tagslogs_sn'] = $value;
					$insert = Db::name('card')->insert($info);
					if(!$insert){
						$status = 0;
						$msg = "编辑失败";
						// 回滚事务
						Db::rollback();
						return ['status' => $status, 'msg' => $msg];
					}
				}
				
			}else{

				foreach ($sn_arr as $key => $value) {
					// 判断对应序列号的保修卡是否存在
					$card_info = Db::name('card')->where('tagslogs_sn',$value)->find();
					if(!empty($card_info)){
						$update = Db::name('card')->where('tagslogs_sn',$value)->update($data);
						if($update === false){
							$status = 0;
							$msg = "编辑失败";
							// 回滚事务
							Db::rollback();
							return ['status' => $status, 'msg' => $msg];
						}
					}else{

						unset($info['id']);
						$info['tagslogs_sn'] = $value;
						$insert = Db::name('card')->insert($info);
						if(!$insert){
							$status = 0;
							$msg = "编辑失败";
							// 回滚事务
							Db::rollback();
							return ['status' => $status, 'msg' => $msg];
						}

					}
					
				}
			}
		}
		adminlog("编辑维修卡", '编辑');
		// 提交事务
    	Db::commit();  
		return ['status' => $status, 'msg' => $msg];
	}

	//查看保修卡
	public function see($id)
	{
		$info = Db::name('card')->where('id', $id)->find();
		$tagslogs = Db::name('tagslogs')->where('sn', $info['tagslogs_sn'])->find();
		if (empty($info)) {
			$info = Db::name('card')->where('sn', 'like', '%' . $info['tagslogs_sn'] . '%')->find();
			$tagslogs = Db::name('tagslogs')->where('sn', $info['tagslogs_sn'])->find();
		}
		$card_record=Db::name('card_record')->where('id',$info['pid'])->find();
		$card_record['bg_img2']=str_replace("\\","/",$card_record['bg_img2']);
		//$info['card_desc'] = Db::name('system')->where('id', 1)->value('card_desc');
		$goodstags = Db::name('goodstags')->where('id', $tagslogs['goodstags_id'])->find();
		$info['model'] = Db::name('good')->where('id', $goodstags['good_id'])->value('model');
		$info['endtime'] = isset($info['endtime']) && $info['endtime'] != 0 ? date('Y-m-d', $info['endtime']) : '暂未激活';
		$this->assign('card_record', $card_record);
		$this->assign('info', $info);
		return $this->fetch();
	}

	public function daochu()
	{
		$data = $this->request->param();
		if (!empty($data['stime']) && !empty($data['etime'])) {
			$data['stime'] = strtotime($data['stime'] . " 00:00:00");
			$data['etime'] = strtotime($data['etime'] . " 23:59:59");
			$lists = Db::name('card')->order('id desc')->where('checkstatus', 1)->where('starttime', '>=', $data['stime'])->where('endtime', '<=', $data['etime'])->select();
		} else {
			$lists = Db::name('card')->where('checkstatus', 1)->order('id desc')->select();
		}
		foreach ($lists as $key => $value) {
			// 查询标签
			$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
			$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
			$good = Db::name('good')->where('id', $good_id)->find();
			$lists[$key]['good_name'] = $good['name'];
			$lists[$key]['goodstags_sn'] = $goodstags['sn'];
			$lists[$key]['good_model'] = $good['model'];
			$lists[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
			$lists[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
			$lists[$key]['status'] = '未过期';
			if (time() > $value['endtime']) {
				$list[$key]['status'] = '已过期';
			}

			switch ($value['checkstatus']) {
				case 0:
					$lists[$key]['checkstatus_name'] = '待审核';
					break;
				case 1:
					$lists[$key]['checkstatus_name'] = '通过';
					break;
				case 2:
					$lists[$key]['checkstatus_name'] = '拒绝';
					break;
			}
		}

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("保修卡数据"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "保修状态")
			->setCellValue("B1", "审核状态")
			->setCellValue("C1", "保修开始")
			->setCellValue("D1", "保修截止")
			->setCellValue("E1", "序列号")
			->setCellValue("F1", "商品名称")
			->setCellValue("G1", "产品型号")
			->setCellValue("H1", "保修卡编号")
			->setCellValue("I1", "单位")
			->setCellValue("J1", "联系人")
			->setCellValue("K1", "电话")
			->setCellValue("L1", "手机")
			->setCellValue("M1", "电子邮件");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['status'])
				->setCellValue("B" . ($key + 2), $val['checkstatus_name'])
				->setCellValue("C" . ($key + 2), $val['starttime'])
				->setCellValue("D" . ($key + 2), $val['endtime'])
				->setCellValue("E" . ($key + 2), $val['goodstags_sn'])
				->setCellValue("F" . ($key + 2), $val['good_name'])
				->setCellValue("G" . ($key + 2), $val['good_model'])
				->setCellValue("H" . ($key + 2), $val['bianhao'])
				->setCellValue("I" . ($key + 2), $val['danwei'])
				->setCellValue("J" . ($key + 2), $val['user'])
				->setCellValue("K" . ($key + 2), $val['phone'])
				->setCellValue("L" . ($key + 2), $val['mobile_phone'])
				->setCellValue("M" . ($key + 2), $val['email']);
			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(30);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="保修卡数据' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}
}
